$(function(){

	CustomersLoad();

});

function CustomersLoad(){
	var c = $("#customers-list")
	$.ajax({
		url: "data/data.xml",
		beforeSend: function(){
			c.html('<li>Molim sačekajte...</li>')
		},
		complete: function(req){
			c.empty();
			if(req.status==200){
				data = $(req.responseXML.childNodes[0]);
				customers = data.find("customer");
				customers.each(function(index){
					var name = $(this).find("name")[0].textContent;
					var uname = encode(name);
					
					var li = $('<li id="'+uname+'" />');
					
					li.append('<a href="javascript:void(0)" onclick="$(this).parent().find(\'.add-set\').toggle()" class="add-btn">+ dodaj dokument</a>')
					li.append('<h2>'+name+'</h2>');
					
					var form = $('<form class="add-set" action="javascript:void(0);" onsubmit="sendDocument(this);" enx-type="multipart/form-data"><p><strong>Odaberite dokument:</b></strong></form>');
					li.append(form);
					
					form.append('<input type="file" name="document" />');
					var btn = $('<input type="submit" value="Pošalji" />');
					form.append(btn);
					
					c.append(li);
				});
			}
		}
	});

}

function encode(string){
	return string.toString().toLowerCase().replace(/[^a-z]/g, "_")
}
function sendDocument(form){
	alert(form.elements['document'].value);
}